package StarMap

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/com_type"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	table_GamePlay "sanguo/node/table/excel/table/GamePlay"
	table_WeaponCollection "sanguo/node/table/excel/table/WeaponCollection"
	cs_message "sanguo/protocol/cs/message"
	"strconv"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionStarMapCollectionAward struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.StarMapCollectionAwardToC
	errcode cs_message.EnumType
}

func (this *transactionStarMapCollectionAward) GetModuleName() string {
	return "StarMap"
}

func (this *transactionStarMapCollectionAward) GetReplyMsg() proto.Message {
	return &cs_message.StarMapCollectionAwardToC{}
}

func (this *transactionStarMapCollectionAward) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionStarMapCollectionAward) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionStarMapCollectionAward) Begin() {

	this.resp = &cs_message.StarMapCollectionAwardToC{}
	msg := this.req.GetData().(*cs_message.StarMapCollectionAwardToS)

	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()
	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(role.GetUserID(), "StarMapCollectionAward : role is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infof("%s %d Call StarMapCollectionAward ", role.GetUserID(), role.GetID())

	//验证功能开启
	if role.GetLev() < table_GamePlay.Table_.IDMap[com_type.GamePaly_WeaponBook].UnlockLevel {
		node_game.Infoln(role.GetUserID(), role.GetID(), "StarMap is not Unlock")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	for _, collection := range msg.GetCollections() {
		cID := collection.GetCollectionId()
		WCDef := table_WeaponCollection.Table_.IDMap[cID]
		if WCDef == nil {
			node_game.Infoln(role.GetUserID(), role.GetID(), "GetStarMapCollection fail:WCDef == nil", cID)
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		c_ := role.GetRoleStarMap().GetStarMap().GetWeaponCollection(cID)
		if c_ == nil {
			node_game.Infoln(role.GetUserID(), role.GetID(), "GetStarMapCollection fail:c_ is nil", cID)
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		if WCDef.Type == int32(6) { //收集类型为武器升星
			weaponId, _ := strconv.Atoi(WCDef.Value)
			weapon := role.GetRoleWeapon().GetWeapon(int32(weaponId))
			if weapon == nil {
				node_game.Infoln(role.GetUserID(), role.GetID(), "GetStarMapCollection fail:weapon is nil", cID)
				this.errcode = cs_message.EnumType_ERROR
				this.user.EndTrans(this)
				return
			}

			if c_.Progress+collection.GetCollectioncount() > weapon.WeaponStar+1 {
				node_game.Infoln(role.GetID(), "GetStarMapCollection fail:get Star fail", cID)
				this.errcode = cs_message.EnumType_ERROR
				this.user.EndTrans(this)
				return
			}
		} else {
			if c_.Progress < collection.GetCollectioncount()*WCDef.Target {
				node_game.Infoln(role.GetUserID(), role.GetID(), "GetStarMapCollection fail 2", cID)
				this.errcode = cs_message.EnumType_ERROR
				this.user.EndTrans(this)
				return
			}
		}

		if !role.GetRoleStarMap().GetWeaponCollectionAward(cID, collection.GetCollectioncount()) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "GetStarMapCollection fail", cID)
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}
	}

	role.OnStarMapDirty()

	node_game.Infoln(role.GetUserID(), role.GetID(), "GetStarMapCollection OK")
	this.errcode = cs_message.EnumType_OK
	this.user.EndTrans(this)
}

func (this *transactionStarMapCollectionAward) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionStarMapCollectionAward) Timeout() {
	this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_RETRY).Enum()
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.StarMapCollectionAwardToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionStarMapCollectionAward{
			user: user,
			req:  msg,
		}
	})
}
